package com.hmall.cart.listenser;

import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单状态消息监听器
 *
 * @author Moozy
 * @since 2025/5/4
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderStatusListener {

    private final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue"),
            exchange = @Exchange(name = "trade.topic", type = ExchangeTypes.TOPIC),
            key = {"order.create"}
    ))
    public void listenOrderSubmitted(List<Long> itemIds, Message message) {
        Long userId = UserContext.getUser();
        log.trace("Here get userId: {}", userId);
        cartService.removeByItemIds(itemIds);
    }

}
